package com.stylefeng.guns.wx.modular.auth.util.mail;/**
 * Package: com.stylefeng.guns.wx.modular.auth.util
 * Description： TODO
 * Author: 4967677099@qq.com
 * Date: Created in 2018/3/18 0018 20:52
 * Version: 0.0.1
 * Modified By: 4967677099@qq.com
 */

import com.stylefeng.guns.wx.config.properties.MailProperties;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.SimpleEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.mail.Session;
import java.util.Properties;

@Component
public class MailUtil {
    @Autowired
    private MailProperties mailProperties;

    public void sendEmail(Mail mail) throws Exception{
//        Email email = new SimpleEmail();
//        email.setHostName("smtp.qq.com");
//        email.setSmtpPort(465);
//        email.setAuthenticator(new DefaultAuthenticator("496767099@qq.com", "nooartdndkbqbghh"));
//        email.setSSLOnConnect(true);
//        email.setFrom("496767099@qq.com", "海挺牛逼");
//        email.setSubject("小说更新啦!");
//        email.setMsg(mail.getMsg());
//        email.addTo(mail.getToEmail());
//        email.send();
        SimpleEmail email = new SimpleEmail();
        email.setHostName(mailProperties.getHostName());
        email.setSmtpPort(Integer.valueOf(mailProperties.getSmtpPort()));
        email.setAuthenticator(new DefaultAuthenticator(mailProperties.getFromEmail(), mailProperties.getPassword()));
        email.setSSLOnConnect("true".equals(mailProperties.getSsl()));
        email.setFrom(mailProperties.getFromEmail(), mailProperties.getNickName());
        email.setSubject("小说更新啦!");
        email.setMsg(mail.getSubject()+mail.getMsg());
        email.addTo(mail.getToEmail());
        email.send();
        System.out.println("发送成功!");
    }
}
